<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="20" class="table">
      <el-col :span="24">
        <el-form :inline="true" size="normal" class="table-form">
          <el-form-item>
            <el-select class="sex" v-model="sex" clearable filterable @change="change">
              <el-option label="男生" :value="1"></el-option>
              <el-option label="女生" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select class="gradeId" v-model="gradeId" clearable filterable @change="change">
              <el-option v-for="item in gradeList" :key="item.id" :label="item.gradeName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12" :offset="0">
        <div ref="bar" id="bar"></div>
      </el-col>
      <el-col :span="12" :offset="0">
        <div ref="pie" id="pie"></div>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import * as echarts from "echarts";
import { adminGetUserCountAPi, adminGetGradeListApi } from "@/api/admin.js";
export default {
  data() {
    return {
      gradeId: null,
      sex: null,
      gradeList: [],
    };
  },
  methods: {
    getUserCount(gradeId = this.gradeId, sex = this.sex) {
      adminGetUserCountAPi({ gradeId, sex }).then((res) => {
        const result = res.data;
        if ((result.status = 200)) {
          let countInfo = result.data;
          this.setBar(countInfo);
          this.setPie(countInfo);
        }
      });
    },
    getGradeList() {
      adminGetGradeListApi().then((res) => {
        const result = res.data;
        if (result.status == 200) {
          this.gradeList = result.data;
        }
      });
    },
    change() {
      this.getUserCount();
    },
    setBar(data) {
      echarts.init(this.$refs.bar).setOption({
        title: {
          text: "各分队人数柱状图",
        },
        tooltip: {},
        xAxis: {
          data: data.xAxis,
        },
        yAxis: {},
        series: [
          {
            name: "人数",
            type: "bar",
            data: data.xData,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "#83bff6" },
                { offset: 0.5, color: "#188df0" },
                { offset: 1, color: "#188df0" },
              ]),
            },
          },
        ],
      });
    },
    setPie(data) {
      echarts.init(this.$refs.pie).setOption({
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "人数",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: data.result,
          },
        ],
      });
    },
  },
  created() {
    this.getUserCount();
    this.getGradeList();
  },
};
</script>

<style scoped>
.table {
  margin-top: 50px;
}
.table .el-col {
  display: flex;
  flex-direction: column;
  align-items: center;
}
#bar {
  width: 600px;
  height: 600px;
}
.sex {
  width: 100px;
}
.gradeId {
  width: 150px;
}
#pie {
  width: 600px;
  height: 600px;
}
</style>